package com.tuangou.tuangouheader.header.service.impl;

import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.tuangou.tuangouheader.header.dao.GroupRecordDao;
import com.tuangou.tuangouheader.header.entity.GroupRecordEntity;
import com.tuangou.tuangouheader.header.service.GroupRecordService;
import com.atguigu.common.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tuangou.tuangouheader.header.dao.*;
import com.tuangou.tuangouheader.header.entity.*;
import com.tuangou.tuangouheader.header.entity.vo.GroupRecord;
import com.tuangou.tuangouheader.header.entity.vo.GroupUserOrderEntityVo;
import com.tuangou.tuangouheader.header.entity.vo.GroupRecordEntityVo;
import com.tuangou.tuangouheader.header.entity.vo.GroupXq;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;


@Service("groupRecordService")
public class GroupRecordServiceImpl extends ServiceImpl<GroupRecordDao, GroupRecordEntity> implements GroupRecordService {

    @Autowired
    private GroupRecordDao groupRecordDao;

    @Autowired
    private RecordUserDao recordUserDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private TuanzhangDao tuanzhangDao;

    @Autowired
    private GroupGoodsDao groupGoodsDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GroupRecordEntity> page = this.page(
                new Query<GroupRecordEntity>().getPage(params),
                new QueryWrapper<GroupRecordEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageInfo<GroupRecordEntityVo> list(Map<String, String> params) {
        PageHelper.startPage(1,3);
        List<GroupRecordEntityVo> list = groupRecordDao.mohu(params);
        PageInfo<GroupRecordEntityVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    @Transactional
    public R updategoods(int rid, int uid) {
        GroupRecordEntity groupRecordEntity = groupRecordDao.selectById(rid);
        TuanzhangEntity tuanzhangEntity = tuanzhangDao.selectById(groupRecordEntity.getTzId());  //订单发起人-团长信息
        GroupGoodsEntity groupGoodsEntity = groupGoodsDao.selectById(groupRecordEntity.getGroupId());  //拼团商品信息

        RecordUserEntity recordUserEntity = new RecordUserEntity(0,rid,uid);
        RecordUserEntity recordUserEntity1 = recordUserDao.findByRidAndUid(rid,uid);
        if (recordUserEntity1!=null){
            return new R().put("msg","你已经参与过了");
        }else {
            if (groupRecordEntity.getCurrentPeonum()<groupGoodsEntity.getPeopleNum()){
                groupRecordEntity.setCurrentPeonum(groupRecordEntity.getCurrentPeonum()+1);
                groupRecordEntity.setStatus(0);
            }else {
                groupGoodsEntity.setStatus(1);
            }
            groupRecordEntity.setCreateDate(new Date());
            groupRecordDao.updateById(groupRecordEntity);
            recordUserDao.insert(recordUserEntity);
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setOrderId(String.valueOf(System.currentTimeMillis())); //订单编号
            orderEntity.setCreateTime(new Date()); //订单创建时间
            orderEntity.setTzId(tuanzhangEntity.getTzId());  //团长信息
            orderEntity.setTzName(tuanzhangEntity.getTzName());
            orderEntity.setTzCity(tuanzhangEntity.getCity());
            orderEntity.setGroupId(groupGoodsEntity.getGroupId());  //拼团商品信息
            orderEntity.setGroupName(groupGoodsEntity.getGroupName());
            orderEntity.setGroupPrice(groupGoodsEntity.getDiscountPrice());
            orderEntity.setNumber(1);  //默认 拼团购买数量 为 1
            orderEntity.setTotalMoney(groupGoodsEntity.getDiscountPrice());  //小计
            orderEntity.setUserId(uid);
            orderEntity.setShopType(1);
            orderEntity.setStatus(1);
            orderDao.insert(orderEntity);
            return new R().put("msg","拼团成功");
        }
    }

    @Override
    public List<GroupUserOrderEntityVo> findListByGid(int gid) {
        return groupRecordDao.findListByGid(gid);
    }

    @Override
    public List<GroupRecord> findAll() {
        return groupRecordDao.findAll();
    }

    @Override
    public List<GroupXq> xianqing(Integer id) {
        return groupRecordDao.xianqing(id);
    }

}